﻿using System;
using System.Collections.Generic;

namespace OTE.Web.Infrastructure.Helper
{
    public static class RandomExtensions
    {
        public static IEnumerable<T> RandomRecord<T>(
           this IEnumerable<T> source, int count, bool allowDuplicates)
        {
            if (source == null) throw new ArgumentNullException("source");
            return RandomRecordIterator<T>(source, count, -1, allowDuplicates);
        }

        public static IEnumerable<T> RandomRecord<T>(
        this IEnumerable<T> source, int count, int seed,
           bool allowDuplicates)
        {
            if (source == null) throw new ArgumentNullException("source");
            return RandomRecordIterator<T>(source, count, seed,
                allowDuplicates);
        }

        private static IEnumerable<T> RandomRecordIterator<T>(IEnumerable<T> source,
            int count, int seed, bool allowDuplicates)
        {
            // take a copy of the current list
            List<T> buffer = new List<T>(source);

            // create the "random" generator, time dependent or with
            // the specified seed
            Random random;
            if (seed < 0)
                random = new Random();
            else
                random = new Random(seed);

            count = Math.Min(count, buffer.Count);

            if (count > 0)
            {
                // iterate count times and "randomly" return one of the
                // elements
                for (int i = 1; i <= count; i++)
                {
                    // maximum index actually buffer.Count -1 because
                    // Random.Next will only return values LESS than
                    // specified.
                    int randomIndex = random.Next(buffer.Count);
                    yield return buffer[randomIndex];
                    if (!allowDuplicates)
                        // remove the element so it can't be selected a
                        // second time
                        buffer.RemoveAt(randomIndex);
                }
            }
        }
    }
}